package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;

import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.GroupChatVote;
import com.kyleduo.switchbutton.SwitchButton;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/1/5
 **/
public class GroupChatVoteCreateActivity extends BaseActivityImpl {
    @BindView(R.id.et_title)
    EditText etTitle;
    @BindView(R.id.sb_multiple)
    SwitchButton sbMultiple;
    @BindView(R.id.ll_options)
    LinearLayout llOptions;
    @BindView(R.id.tv_add_option)
    TextView tvAddOption;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;

    private int dp12;

    public static void show(Context context, int groupChatId, ActivityResultLauncher<Intent> launcher) {
        Intent intent = new Intent(context, GroupChatVoteCreateActivity.class);
        intent.putExtra("groupChatId", groupChatId);
        launcher.launch(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_create_group_chat_vote;
    }

    @Override
    public String getTitleStr() {
        return "发布投票";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dp12 = SizeUtils.dp2px(12);
        for (int i = 0; i < 2; i++) {
            addOption();
        }

        tvAddOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (llOptions.getChildCount() < 15) {
                    addOption();
                } else {
                    Toast.makeText(mContext, "最多添加15个选项", Toast.LENGTH_SHORT).show();
                }
            }
        });

        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(etTitle.getText())) {
                    Toast.makeText(mContext, "请输入主题", Toast.LENGTH_SHORT).show();
                    return;
                }

                ArrayList<String> options = new ArrayList<>();
                for (int i = 0; i < llOptions.getChildCount(); i++) {
                    LinearLayout linearLayout = (LinearLayout) llOptions.getChildAt(i);
                    EditText et = (EditText) linearLayout.getChildAt(0);
                    if (!TextUtils.isEmpty(et.getText())) {
                        options.add(et.getText().toString());
                    }
                }

                if (options.size() == 0) {
                    Toast.makeText(mContext, "投票最少需要两个选择", Toast.LENGTH_SHORT).show();
                    return;
                }

                HashMap<String, Object> params = new HashMap<>();
                params.put("id", getIntent().getIntExtra("groupChatId", 0));
                params.put("subject", etTitle.getText().toString());
                params.put("is_multiple", sbMultiple.isChecked());
                params.put("options_str", options);

                requestManager.request(requestManager.mMessageRetrofitService.createGroupChatVote(RequestManager.getJsonRequestBody(params)),
                        new BaseObserverImpl<GroupChatVote>(mContext) {
                            @Override
                            public void onSuccess(GroupChatVote groupChatVote) {
                                super.onSuccess(groupChatVote);
                                setResult(RESULT_OK);
                                finish();
                            }
                        });
            }
        });
    }

    private void addOption() {
        LinearLayout llItem = new LinearLayout(mContext);
        llItem.setGravity(Gravity.CENTER_VERTICAL);
        llItem.setOrientation(LinearLayout.HORIZONTAL);
        llItem.setPadding(0, dp12, 0, 0);

        EditText et = new EditText(mContext);
        et.setHint("选项" + (llOptions.getChildCount() + 1) + "，40字以内");
        et.setTextAppearance(R.style.NormalTextView);
        et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(40)});
        et.setBackgroundResource(R.drawable.shape_bg_corner_gray_light);
        et.setPadding(dp12, dp12, dp12, dp12);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMarginEnd(dp12);
        layoutParams.weight = 1;

        llItem.addView(et, layoutParams);

        ImageView ivDelete = new ImageView(mContext);
        ivDelete.setImageResource(R.mipmap.icon_x_oval_red);
        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (llOptions.getChildCount() > 2) {
                    llOptions.removeView(llItem);

                    for (int i = 0; i < llOptions.getChildCount(); i++) {
                        LinearLayout linearLayout = (LinearLayout) llOptions.getChildAt(i);
                        EditText et = (EditText) linearLayout.getChildAt(0);
                        et.setHint("选项" + (i + 1) + "，40字以内");
                    }
                } else {
                    et.setText("");
                    Toast.makeText(mContext, "投票最少需要两个选择", Toast.LENGTH_SHORT).show();
                }
            }
        });
        llItem.addView(ivDelete);

        llOptions.addView(llItem);
    }
}







































